﻿{{ extend '../_layouts/user.html'}}
{{ block 'left' }}
<ul id="leftbar">
    <li><a href="/userset">基本信息</a></li>
    <li class="now"><a href="/safeset">安全中心</a></li>
    <li>其它</li>
</ul>
{{ /block }}


{{ block 'right' }}
<form id="form-submit">
    <div class="changepassword">
        <label>
            邮&nbsp;&nbsp;&nbsp;&nbsp;箱
            <input type="text" name="email" id="email" class="my-form" value="{{ user.email }}">
        </label><br>
        <label>
            旧密码
            <input type="password" name="oldpassword" id="password" class="my-form" value="">
        </label><br>
        <label>
            新密码
            <input type="password"  id="newpassword1" class="my-form" value="">
        </label><br>
        <label class="password">
            请确认
            <input type="password" name="password" id="newpassword2" class="my-form" value="">
            <span id="password-info"></span>
        </label> 
        <br>
       
    </div>
    <button type="submit" class="btn btn-default submit">修改密码</button>
</form>
{{ /block }}


{{ block 'script' }}
<script>
    // $('#form-submit').on('submit', function (e) {
    //   e.preventDefault()
    //   var formData = $(this).serialize()
    //   $.ajax({
    //     url: '/userset',
    //     type: 'post',
    //     data: formData,
    //     dataType: 'json',
    //     success: function (data) {
    //       if(data.err_code==0){
    //           window.alert('修改成功！')
    //           window.location.href = '/userset'
    //       }else{
    //           window.alert('修改失败！')
    //       }
    //     }
    //   })
    $('#newpassword2').on('keyup',function(){
        if($(this).val()!==$('#newpassword1').val()){
            $('#password-info').text('两次密码输入不一致')
        }else{
            $('#password-info').text('')
        }
    })
      $('#form-submit').on('submit',function(e){
          e.preventDefault()
          if($('#password-info').text()!==''){
              return window.alert('两次密码输入不一致，请修改')
          }
          let formData = $(this).serialize()
          $.ajax({
              url:'/safeset',
              type:'post',
              data:formData,
              dataType:'json',
              success(data){
                  if(data.err_code == 0){
                      window.alert('修改成功！请重新登录...')
                      window.location.href = '/login'
                  }else if(data.err_code == 1){
                    window.alert('您输入的原密码有误！')
                    window.location.href = '/safeset'
                  }else{
                    window.alert('修改失败！')
                    window.location.href = '/safeset'
                  }
              }
          })
      })
</script>
{{ /block }}